/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.andersen.web.controller;

import com.andersen.database.entity.Roles;
import com.andersen.database.entity.UserEntity;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Corvus 11
 */
@Controller
public class SelectUserController extends AbstractController{
    
    @RequestMapping(value = "/select_user", method = RequestMethod.GET)
    public String openSelectPage(ModelMap map) {
        
        if (!hasGrantedAuthority(Roles.ADMIN)) {
            return "index";
        }
        return "select_user";
    }
    
    @RequestMapping(value = "/k_select_user", method = RequestMethod.GET)
    public String openKendoSelectPage(ModelMap map) {
        
        if (!hasGrantedAuthority(Roles.ADMIN)) {
            return "index";
        }
        return "k_select_user";
    }
    
    @RequestMapping(value = "/a_select_user", method = RequestMethod.GET)
    public @ResponseBody JSONObject getUserList(HttpServletRequest req, HttpServletResponse response) throws IOException {
        
        int offset = requestUtils.getIntegerParam(req, PARAM_NAME_DISPLAY_START);
        int count = requestUtils.getIntegerParam(req, PARAM_NAME_DISPLAY_COUNT);
        int sortNum = requestUtils.getIntegerParam(req, PARAM_NAME_SORT_BY);
        int sortDir = (PARAM_VAL_SORT_ASC.equals(req.getParameter(PARAM_NAME_SORT_ORDER))) ? 
                userService.SORT_DIRECTION_ASC : userService.SORT_DIRECTION_DESC;
       
        ArrayList<UserEntity> users = userService.queryUsers(offset, count, sortNum, sortDir);
        
        JSONArray arr = new JSONArray();
        for(int i=0;i<users.size();i++) {
            JSONObject jobj = dataTableJsonUtils.UserToDataTableRow(users.get(i));
            arr.add(jobj);
        }
        
        JSONObject result = new JSONObject();
        result.accumulate(RESULT_ATTRIBUTE_DATA, arr);
        result.accumulate(RESULT_ATTRIBUTE_ECHO, req.getParameter(PARAM_NAME_ECHO)); 
        long tableSize = userService.getUserCount();
        result.accumulate(RESULT_ATTRIBUTE_TOTAL_RECORDS, String.valueOf(tableSize));
        result.accumulate(RESULT_ATTRIBUTE_DISPLAY_RECORDS, String.valueOf(tableSize));
        
        return result;
    }
    
    @RequestMapping(value = "/a_load_rights_dialog", method = RequestMethod.GET)
    public String loadRightsDialog(@RequestParam(value = "id", required=true) String userId,
            ModelMap map) throws IOException {
            
        UserEntity user = userService.getUser(Long.parseLong(userId));
        
        map.addAttribute("userId", userId);
        if (user.getRoles().contains(Roles.HR)) map.addAttribute("checked_hr", "checked");
        if (user.getRoles().contains(Roles.MANAGER)) map.addAttribute("checked_manager", "checked");
        
        return "templates/dialog_rights";
        
    }
    
    @RequestMapping(value = "/a_change_roles", method = RequestMethod.POST)
    public void changeRoles(@RequestParam(value = "id", required = true) String id,
                            @RequestParam(value = "hr", required = true) String hr,
                            @RequestParam(value = "manager", required = true) String manager,
                            HttpServletResponse response) {
        
        //LOG.info("[/a_change_roles] mapped");
        
        long userId = Long.parseLong(id);
        boolean isHr = Boolean.parseBoolean(hr);
        boolean isManager = Boolean.parseBoolean(manager);
        
        if (isHr) userService.addRole(userId, Roles.HR);
        else userService.dropRole(userId, Roles.HR);
        
        if (isManager) userService.addRole(userId, Roles.MANAGER);
        else userService.dropRole(userId, Roles.MANAGER);
        
        //LOG.info("[/a_change_roles] finished");
        
        response.setStatus(200);
    }
    
    @RequestMapping(value = "/a_activate_user", method = RequestMethod.POST)
    public void setUserActivity(@RequestParam(value = "id", required = true) String id,
                                @RequestParam(value = "active", required = true) String active,
                                HttpServletResponse response) {
        //LOG.info("[/a_activate_user] mapped");
        long userId = Long.parseLong(id);
        boolean userActive = "1".equals(active);
        if (userService.setUserActivity(userId,userActive)) response.setStatus(200);
        else response.setStatus(500);
    }
    
    
}
